package cn.onesdream.app.api.controller;

import cn.onesdream.app.api.service.CollectionService;
import cn.onesdream.app.common.exception.ServiceException;
import cn.onesdream.app.common.util.ResponseResult;
import cn.onesdream.app.dao.entity.Collection;
import cn.onesdream.app.dao.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author WangShiyu
 * @date 2021/3/20 13:55
 */
@RestController
@RequestMapping("/api/collection")
@Slf4j
@Api(tags = "收藏相关接口")
@Validated
public class CollectionController {
    @Autowired
    private CollectionService collectionService;

    @ApiOperation("通过objectId删除收藏内容")
    @PostMapping("/deleteCollectionByObjectId")
    public ResponseResult<Collection> deleteCollectionByObjectId(Byte typeId, Integer objectId) throws ServiceException {
        Integer userId = ((User) SecurityContextHolder.getContextHolderStrategy().getContext().getAuthentication().getPrincipal()).getId();
        collectionService.deleteCollectionByObjectId(typeId, objectId, userId);
        return new ResponseResult<>();
    }

    @ApiOperation("删除收藏内容")
    @PostMapping("/deleteCollection")
    public ResponseResult<Collection> deleteCollection(Integer collectionId) throws ServiceException {
        Integer userId = ((User) SecurityContextHolder.getContextHolderStrategy().getContext().getAuthentication().getPrincipal()).getId();
        collectionService.deleteCollection(collectionId, userId);
        return new ResponseResult<>();
    }

    @ApiOperation("通过userId获取收藏信息")
    @PostMapping("/getCollection")
    public ResponseResult<List> getCollection(Integer userId){
        List<Collection> collectionList = collectionService.getCollection(userId);
        return new ResponseResult<>(collectionList);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "typeId", value = "收藏类型，1：卡片，2：文章"),
            @ApiImplicitParam(name = "objectId", value = "卡片id或文章id")
    })
    @ApiOperation("收藏卡片或文章")
    @PostMapping("/collect")
    public ResponseResult<Collection> collection(Byte typeId, Integer objectId) throws ServiceException {
        Integer userId = ((User) SecurityContextHolder.getContextHolderStrategy().getContext().getAuthentication().getPrincipal()).getId();
        collectionService.collect(userId, typeId, objectId);
        return new ResponseResult<>();
    }
}
